<?php

define('NO_DIRECT',true);
define('ROOT_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);
define('CONTROLLER_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR.'controller'.DIRECTORY_SEPARATOR);

session_start();
date_default_timezone_set('Asia/Jakarta');
require_once(ROOT_PATH.'cn-functions.php');

$con = open_db();
if (isset($_GET['p'])) {
	if ($_GET['p'] == 'signin-action') {
		$session = check_session();
		if ($session['is_session_valid']) {
			echo 'redir to beranda bitch';
		} else {
			require_once(CONTROLLER_PATH.'signin-action.php');
		}
	} else {
		if (file_exists(CONTROLLER_PATH.$_GET['p'].'.php')) {			
			$session = check_session();
			if ($session['is_session_valid']) {
				// give the shit
				require_once(CONTROLLER_PATH.$_GET['p'].'.php');
			} else {
				// redir to login page bitch
				echo 'redir bitch';
			}
		} else {
			// 404 bitch
			echo '404 bitch';
		}
	}
} else {
	// yknow
	$session = check_session();
	if ($session['is_session_valid']) {
		echo 'redir to beranda bitch';
	} else {
		echo '<form action="'.make_url('signin-action','').'" method="post">
		<input type="text" name="username" />
		<input type="password" name="password" />
		<button>Sign In</button>';
		if (isset($_GET['r'])) {
			echo '<input type="hidden" name="redirect" value="'.urlencode($_GET['r']).'" />';
		}
		echo '</form>';
	}
}
close_db($con);